/**
 * Created by sunkejun on 2015/10/15.
 */

$(document).ready(function(){
    //console.log("htmk"+$('.summernote').html());
   $('#post_content').summernote({
        onImageUpload: function(files,editor,welEditable) {
            console.log('image upload:', files);
            admin_post_edit.sendFile(files[0], editor, welEditable);
        }
   });
    //$('.summernote').code($('.summernote').html());
    $('.i-checks').iCheck({
        checkboxClass: 'icheckbox_square-green',
        radioClass: 'iradio_square-green',
    });


    $("#category_tags").select2({ width: '100%' });
    admin_post_edit.initselect2();

    $("#history").change(function(){
        window.location.href = "/admin/post/create?"+this.value;
    });

})

var admin_post_edit  = (function ($) {
    //发布
    this.public = function() {
        save(1);
    }
    this.sendFile = function(file, editor, welEditable) {
        data = new FormData();
        data.append("file", file);
        $.ajax({
            data: data,
            type: "POST",
            url: "/admin/file/uploadSummernotefile",
            cache: false,
            dataType: 'json',
            contentType: false,
            processData: false,
            success: function(result) {

                if(result.code == 200){
                    editor.insertImage(welEditable, result.url);
                }

            }
        });
    }

    this.initselect2 = function() {
        ComdataUtil.ajaxpostnormal('/admin/tag/-1',{},function(data){
            //$exampleMulti.val(["CA", "AL"]).trigger("change");
                $('#category_tags').select2({tags: true, width: '100%',data: data});
                var vals = $('#category_tags').attr('value');
                if(vals != null && vals != ""){
                    $('#category_tags').val(vals.split(',')).trigger("change");
                }

        },'GET');
    }
    //保存草稿
    this.draft = function() {
        save(0);
    }
    //保存
    this.save = function(type) {
        $('#com_message').html("");
        var target = $("div.note-editor button[data-event='codeview']");
        if($(target).hasClass('active')){
            $("div.note-editor button[data-event='codeview']").click();

        }

        var data = ComdataUtil.collectData("post_content_div");
        data.post_content =$('#post_content').code();
        data.post_status = type;
        ComdataUtil.ajaxpost('/admin/post',data,function(data){
            if(data.code == 500){
                $('#com_message').attr("class","text-danger text-center");
                $('#com_message').html(data.res_data);
            }else{
                //window.location.href = "/admin/post";
                //ComdataUtil.clear("post_content_div");
                //$('#use_seo').iCheck('uncheck');
                //$('#category_tags').val(null).trigger("change");
                //$('#post_content').code("");
            }


        });
    }
    this.openModel = function(){
        $("#category_modal").modal('show');
        $.ajax({
            url:  '/admin/category/create',
            type:"GET",
            dataType:'JSON',
            data: {},
            success:function(response){
                initTree(response);
            }
        });
    }
    this.initTree = function(data){
        var setting = {
            callback: {
                onClick: this.treeCategorySelect
            },
            data: {
                simpleData: {
                    enable: true,
                    idKey: "id",
                    pIdKey: "parent_id"
                }
            }
        };
        var zNodes =data;
        this.$tree = $.fn.zTree.init($("#category_tree"), setting, zNodes);

    }
    this.treeCategorySelect = function(event, treeId, treeNode){
        //alert(treeNode.tId + ", " + treeNode.name + "," + treeNode.checked);

        $("#category_modal_current").html(treeNode.name);

    }

    this.deleteHistory = function(historyid){
        swal({
            title: language.getNorlanguage().delete_title,
            type: "warning",
            showCancelButton: true,
            confirmButtonColor: "#DD6B55",
            confirmButtonText: language.getNorlanguage().delete_sub,
            cancelButtonText: language.getNorlanguage().delete_cancle,
            closeOnConfirm: true,
            html: false
        }, function(){
            ComdataUtil.ajaxpostnormal('/admin/post/deletePostHistory',{id:historyid},function(data){
                if(data.code == 200){
                    openToastr.open(1,data.res_data);
                    $("#histroy"+historyid).remove();
                }
            },"POST");
        });
    }
    this.selectNode = function(){
        var nodes = this.$tree.getSelectedNodes();
        if(nodes.length >0){
            $("#category_name").val(nodes[0].name);
            $("#category_id").val(nodes[0].id);
        }
        $("#category_modal").modal('hide');
    }
    return this;
})(jQuery);

